package com.suyang.syt.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suyang.syt.auth.AuthClass;
import com.suyang.syt.auth.AuthMethod;
import com.suyang.syt.model.User;
import com.suyang.syt.model.dto.AjaxObj;
import com.suyang.syt.model.dto.WeixinMenuDTO;
import com.suyang.syt.service.interfaces.WeixinMenuService;
import com.suyang.syt.util.DateFormat;
import com.suyang.syt.util.EmptyUtil;
import com.suyang.syt.util.MyException;
import com.suyang.syt.util.StringUtil;
import com.suyang.syt.weixin.WeixinKit;

@Controller
@RequestMapping("/admin/weixin/menu")
@AuthClass(value = "login")
public class WeixinMenuController {

	@Autowired
	private WeixinMenuService weixinMenuService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj create() {
		try {
			List<WeixinMenuDTO> datas = weixinMenuService.addAndCreate();
			if (WeixinKit.createMenu(datas)) {
				return new AjaxObj(1, "微信菜单创建成功!");
			}
		} catch (MyException e) {
			return new AjaxObj(0, e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxObj(0, StringUtil.ERROR_MSG);
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@AuthMethod(role = "ROLE_WEIXIN")
	public String list() {
		return "weixin/menu/list";
	}

	@RequestMapping(value = "/listTreegrid", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public List<WeixinMenuDTO> listTreegrid(HttpServletRequest request, String id) {
		try {
			long mid = 0;
			if (EmptyUtil.isNotEmpty(id)) {
				mid = Long.parseLong(id);
			}
			return weixinMenuService.listTreegrid(mid);
		} catch (Exception e) {
			return null;
		}
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj add(HttpServletRequest request, WeixinMenuDTO weixinMenuDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			weixinMenuDTO.setCreateName(user.getUsername());
			weixinMenuDTO.setCreateTime(DateFormat.dateToString(new Date()));
			return weixinMenuService.add(weixinMenuDTO);
		} catch (MyException e) {
			return new AjaxObj(0, e.getMessage());
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj delete(String ids, HttpServletRequest request) {
		try {
			return weixinMenuService.deletes(ids);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@AuthMethod(role = "ROLE_WEIXIN")
	@ResponseBody
	public AjaxObj update(HttpServletRequest request, WeixinMenuDTO weixinMenuDTO) {
		try {
			User user = (User) request.getSession().getAttribute("loginUser");
			weixinMenuDTO.setModifyName(user.getUsername());
			weixinMenuDTO.setModifyTime(DateFormat.dateToString(new Date()));
			return weixinMenuService.update(weixinMenuDTO);
		} catch (Exception e) {
			return new AjaxObj(0, StringUtil.ERROR_MSG);
		}
	}

}
